
import { imgcdn } from '@/utils/config'
import { ElMessage, ElMessageBox } from 'element-plus';
export const validatePhone = (phoneNumber) => {
  const phoneRegex = /^1[3-9]\d{9}$/;
  return phoneRegex.test(phoneNumber);
}

export const debounce = (fn, delay) => {
  let timer = null;
  let isFirst = true;

  return function (...args) {
    if (timer) clearTimeout(timer);

    if (isFirst) {
      fn.apply(this, args);
      isFirst = false;
      return;
    }

    timer = setTimeout(() => {
      fn.apply(this, args);
    }, delay);
  }
}

export const imgUrls = (imgs) => {
  const reg = /^(http|https)/;
  if (!imgs) return;
  if (imgs.search(reg) != -1) return imgs;
  return imgcdn + imgs
}


// 下载文件
export const downloadFile = (url) => {
  if (!url) return ElMessage.warning('暂未找到相关文件');
  const downloadUrl = imgUrls(url);
  const link = document.createElement('a');
  link.href = downloadUrl;
  link.download = url;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  ElMessage.success('下载成功');
}

// 打开文件
export const openFile = (url) => {
  if (!url) return ElMessage.warning('暂未找到相关文件');
    const openUrl = imgUrls(url);
    window.open(openUrl, '_blank');
}